package com.h3c.ptability.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.h3c.it.common.misc.lang.Assert;
import com.h3c.ptability.cache.UserCache;
import com.h3c.ptability.dto.LoginUserDTO;
import com.h3c.ptability.dto.PtMsgTemplateDTO;
import com.h3c.ptability.entity.PtMsgTemplate;
import com.h3c.ptability.mapper.PtMsgTemplateMapper;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.EosAuthService;
import com.h3c.ptability.service.IPtMsgTemplateService;
import com.h3c.ptability.utils.AppBusinessException;
import com.h3c.ptability.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: PtMsgTemplateServiceImpl
 *
 * @author l22886
 * @date 2022-03-14
 * Description: pt_msg_template serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class PtMsgTemplateServiceImpl extends ServiceImpl<PtMsgTemplateMapper, PtMsgTemplate> implements IPtMsgTemplateService {

    @Resource
    private PtMsgTemplateMapper ptMsgTemplateMapper;

    @Autowired
    private CommService commService;

    @Override
    public List<PtMsgTemplateDTO> selectFuzzyByDto(PtMsgTemplateDTO dto) {
        List<PtMsgTemplate> result = new ArrayList<>();
        UserCache.checkLoginUser(dto.getAppId());
        LoginUserDTO loginUser = UserCache.getLoginUserDTO();
        String account = loginUser.getAccount();
        String userId = loginUser.getUserId();
        PtMsgTemplate condition = JSONObject.parseObject(JSONObject.toJSONString(dto), PtMsgTemplate.class);
        if ("e-sign-platform".equals(dto.getAppId()) && !isSSC(dto.getAppId(), account)) {
            condition.setCreatorInfo(userId);
        }
        result = ptMsgTemplateMapper.queryListByFuzzy(condition);
        //List<PtMsgTemplate> list = ptMsgTemplateMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto), PtMsgTemplate.class));
        return JSONArray.parseArray(JSONArray.toJSONString(result), PtMsgTemplateDTO.class);
    }

    public boolean isSSC(String appId, String account) {
        boolean isSSC = false;
        EosAuthService eosService = SpringUtil.getBean(EosAuthService.class);
        List<String> roles = eosService.getUserRoleByAccount(account, appId);
        for (String role : roles) {
            if (role.equals("SSC-SIGN")) {
                isSSC = true;
                break;
            }
        }
        return isSSC;
    }

    @Override
    public List<PtMsgTemplateDTO> selectByDto(PtMsgTemplateDTO dto) {
        return null;
    }

    @Override
    public Integer insertDto(PtMsgTemplateDTO dto) {
        UserCache.checkLoginUser(dto.getAppId());

        if (null == dto.getMsgTemplateId() || "".equals(dto.getMsgTemplateId())) {
            LambdaQueryWrapper<PtMsgTemplate> query = new QueryWrapper<PtMsgTemplate>().lambda().eq(PtMsgTemplate::getMsgTemplateCode, dto.getMsgTemplateCode());
            Assert.isNull(getOne(query), CommonUtil.errorMsg("邮件模板不能重复"));
            String pkId = IdWorker.nextId() + "";
            dto.setMsgTemplateId(pkId);
            dto.setCreatorInfo(UserCache.getLoginUserNameAndId());
        }
        dto.setUpdatorInfo(UserCache.getLoginUserNameAndId());
        dto.setUpdateTime(new Date());
        return saveOrUpdate(JSONObject.parseObject(JSON.toJSONString(dto), PtMsgTemplate.class)) ? 1 : 0;
    }

    @Override
    public Integer updateDto(PtMsgTemplateDTO dto) {
        UserCache.checkLoginUser(dto.getAppId());

        dto.setUpdatorInfo(UserCache.getLoginUserNameAndId());
        dto.setUpdateTime(new Date());
        return ptMsgTemplateMapper.updateById(JSONObject.parseObject(JSON.toJSONString(dto), PtMsgTemplate.class));
    }

    @Override
    public Integer deleteDto(PtMsgTemplateDTO dto) {
        UserCache.checkLoginUser(dto.getAppId());
        String msgTemplateId = dto.getMsgTemplateId();
        PtMsgTemplate template = getById(msgTemplateId);
        if (template.getSystemEmail().equals(1)){
            throw  new AppBusinessException("系统邮件模板不可删除");
        }
        return ptMsgTemplateMapper.deleteById(dto.getMsgTemplateId());
    }
}
